package com.goodcol.plugin.dto;

import com.goodcol.plugin.entity.ColumnConfig;
import com.goodcol.plugin.entity.ColumnConfigGroup;
import com.goodcol.plugin.tool.DocCommentUtils;
import com.goodcol.plugin.tool.JSON;
import com.goodcol.plugin.tool.NameUtils;
import com.goodcol.plugin.tool.PsiClassGenerateUtils;
import com.goodcol.plugin.tool.SettingGroupUtils;
import com.goodcol.plugin.tool.TypeMapperUtils;
import com.intellij.database.model.DasColumn;
import com.intellij.database.util.DasUtil;
import com.intellij.psi.PsiField;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 列信息传输对象
 *
 * @author QuickCode
 * @version 1.0.0
 * @date 2024-02-03 12:34:25
 */
@Data
@NoArgsConstructor
public class ColumnInfoDTO {

    /**
     * 名称
     */
    private String name;
    /**
     * 注释（界面中可以修改保存）
     */
    private String comment;
    /**
     * 原始注释（数据库字段的原始注释）
     */
    private String originComment;
    /**
     * 全类型
     */
    private String type;
    /**
     * 是否主键
     */
    private Boolean isPrimaryKey;
    /**
     * 标记是否为自定义附加列
     */
    private Boolean custom;
    /**
     * 扩展数据(JSON字符串)
     */
    private String ext;

    /**
     * ColumnInfoDTO
     *
     * @param field             field
     * @param templateGroupName templateGroupName
     */
    public ColumnInfoDTO(PsiField field, String templateGroupName) {
        this.name = field.getName();
        this.comment = DocCommentUtils.getComment(field.getDocComment());
        this.originComment = comment;
        this.type = field.getType().getCanonicalText();
        this.isPrimaryKey = PsiClassGenerateUtils.isPkField(field);
        this.custom = false;
        this.ext = getDefaultExtValues(templateGroupName);
    }

    /**
     * ColumnInfoDTO
     *
     * @param column            column
     * @param templateGroupName templateGroupName
     */
    public ColumnInfoDTO(DasColumn column, String templateGroupName) {
        this.name = NameUtils.getInstance().getJavaName(column.getName());
        this.comment = column.getComment();
        this.originComment = comment;
        this.type = TypeMapperUtils.getJavaType(column.getDasType().toDataType().toString(), templateGroupName);
        this.isPrimaryKey = DasUtil.isPrimary(column);
        this.custom = false;
        this.ext = getDefaultExtValues(templateGroupName);
    }

    /**
     * getDefaultExtValues
     *
     * @param groupName groupName
     * @return String
     */
    private String getDefaultExtValues(String groupName) {
        ColumnConfigGroup columnConfigByGroup = SettingGroupUtils.getColumnConfigByGroup(groupName);
        if (columnConfigByGroup == null || columnConfigByGroup.getElementList() == null || columnConfigByGroup.getElementList().isEmpty()) {
            return "{}";
        }
        List<ColumnConfig> elementList = columnConfigByGroup.getElementList();
        Map<String, Object> defaultExtMap = new HashMap<>(elementList.size());
        for (ColumnConfig columnConfig : elementList) {
            String extName = columnConfig.getTitle();
            String defaultValue = columnConfig.getDefaultValue() == null ? "" : columnConfig.getDefaultValue();
            switch (columnConfig.getType()) {
                case TEXT:
                    defaultExtMap.put(extName, defaultValue);
                    break;
                case BOOLEAN:
                    defaultExtMap.put(extName, "true".equalsIgnoreCase(defaultValue) ? Boolean.TRUE : Boolean.FALSE);
                    break;
                case SELECT:
                    defaultExtMap.put(extName, defaultValue.split(",")[0]);
                    break;
            }
        }
        return JSON.toJson(defaultExtMap);
    }

}
